package org.jacp.demo.ui.fw;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.Timer;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.jacp.api.action.IAction;
import org.jacp.api.componentLayout.Layout;
import org.jacp.demo.entities.Employee;
import org.jacp.demo.services.EmployeeService;
import org.jacp.demo.ui.EmployeeTableModel;
import org.jacp.demo.ui.fw.components.EmployeeForm;
import org.jacp.swing.rcp.component.ASwingComponent;

import com.explodingpixels.macwidgets.IAppWidgetFactory;
import com.explodingpixels.macwidgets.MacWidgetFactory;

public class EmployeeEditor extends ASwingComponent {
	private JSplitPane main = null;

	private JTable table;
	static List<Employee> employees;
	private EmployeeTableModel tableModel;
	private EmployeeForm form;
	private EmployeeService employeeService;

	private boolean lock = false;
	private int currentLimit = 200;
	private JPanel panel;
	private boolean init = true;

	/**
	 * Create the panel.
	 */
	public EmployeeEditor() {

	}

	@Override
	public void handleBarEntries(final Map<Layout, Container> bars) {
		final Container bottomBar = bars.get(Layout.SOUTH);
		final JButton add = new JButton(new ImageIcon(Toolkit
				.getDefaultToolkit().getImage("NSImage://NSAddTemplate")));
		add.putClientProperty("JButton.buttonType", "segmentedTextured");
		add.putClientProperty("JButton.segmentPosition", "first");
		add.setFocusable(false);
		add.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(final ActionEvent arg0) {
				lock = true;
				final int LIMIT = main.getDividerSize(); // full collapsed
				final int LIMIT_2 = 200; // full extend
				final int INTERVAL = 1;
				final boolean reverse = (main.getWidth() - main
						.getDividerLocation()) == LIMIT ? true : false;
				currentLimit = reverse ? LIMIT_2 : LIMIT;
				final Timer timer = new Timer(INTERVAL, null);
				final ActionListener listener = new ActionListener() {

					@Override
					public void actionPerformed(final ActionEvent arg0) {
						final int loc = (main.getWidth() - main
								.getDividerLocation());
						if (!reverse) {
							if (loc > LIMIT) {
								main.setDividerLocation(main
										.getDividerLocation() + 2);
							} else {
								if (panel.isVisible())
									panel.setVisible(false);
								timer.stop();
							}
						} else {
							if (!panel.isVisible())
								panel.setVisible(true);
							if (loc < LIMIT_2) {
								main.setDividerLocation(main
										.getDividerLocation() - 2);
							} else {

								timer.stop();
							}
						}

					}
				};
				timer.addActionListener(listener);
				timer.start();
				lock = false;
			}
		});

		bottomBar.add(add);
	}

	@Override
	public void handleMenuEntries(final Container arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public Container handleAction(final IAction<ActionEvent, Object> action) {
		if (main == null) {
			main = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);

			initTable();

			final JScrollPane scrollPane = new JScrollPane(table);
			IAppWidgetFactory.makeIAppScrollPane(scrollPane);
			scrollPane.setSize(new Dimension(824, 800));

			main.add(scrollPane);
			main.setBorder(null);
			panel = new JPanel();
			form = new EmployeeForm();
			panel.add(form);
			main.add(panel);

			// set locations
			main.addComponentListener(new ComponentListener() {

				@Override
				public void componentShown(ComponentEvent arg0) {
					main.setDividerLocation(main.getWidth() - 200);
				}

				@Override
				public void componentResized(ComponentEvent arg0) {
					if (init) {
						main.setDividerLocation(main.getWidth() - 200);
						init = false;
					}
					if (!lock)
						main.setDividerLocation(main.getWidth() - currentLimit);
				}

				@Override
				public void componentMoved(ComponentEvent arg0) {
					// TODO Auto-generated method stub

				}

				@Override
				public void componentHidden(ComponentEvent arg0) {
					// TODO Auto-generated method stub

				}
			});
		}
		return main;
	}

	public void initTable() {

		final String[] columnNames = { "ID", "FIRSTNME", "MIDINIT", "LASTNAME",
				"PHONENO", "JOB", "EDLEVEL", "SEX", "SALERY", "BONUS",
				"COMMISSION" };
//		employees = employees == null ? employeeService.findAll() : employees;
//		tableModel = new EmployeeTableModel(columnNames,
//				(List<Employee>) (employees));
		 tableModel = new EmployeeTableModel(columnNames,
		 (List<Employee>) (Collections.EMPTY_LIST));
		// table = new JTable();
		table = MacWidgetFactory.createITunesTable(tableModel);
		// table.setAutoCreateRowSorter(true);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		final ListSelectionModel listSelectionModel = table.getSelectionModel();
		listSelectionModel
				.addListSelectionListener(new ListSelectionListener() {
					@Override
					public void valueChanged(final ListSelectionEvent e) {
						if (!e.getValueIsAdjusting()) {
							final int selectedRow = table.getSelectedRow();
							if (selectedRow >= 0) {
								final Employee emp = tableModel
										.getRecordAt(selectedRow);
								form.setEmployee(emp);
							}
						}
					}

				});
		table.setModel(tableModel);
		// table.setSurrendersFocusOnKeystroke(true);
		// if (!tableModel.hasEmptyRow()) {
		// tableModel.addEmptyRow();
		// }

	}

	public EmployeeService getEmployeeService() {
		return employeeService;
	}

	public void setEmployeeService(EmployeeService employeeService) {
		this.employeeService = employeeService;
	}

}
